'''
Created on Jan 18, 2010

@author: christianp
'''
from Extent import Extent

class Utilities(object):
    
    def __init__(self):
        '''Utilities'''
    
    @staticmethod
    def getNearestLevel(resolutions, resolution):
        
        if (len(resolutions) == 0):
            raise Exception('Arguments', 'Min should be smaller then max')
        
        #smaller then smallest
        if (resolutions[(len(resolutions) - 1)][1] > resolution):
            return len(resolutions) - 1
        
        #bigger then biggest
        if (resolutions[0][1] < resolution):
            return 0
        
        for i in range(0, len(resolutions)):
            reso = resolutions[i]
            if (reso[1] == resolution):
                return reso[0]
        '''
        result = 0
        result_distance = 9999999999#abs(resolutions[0] - resolution)
        for i in range(0,len(resolutions)):
            distance = abs(resolutions[i][1] - resolution)
            if (distance < result_distance):
                result = i
                result_distance = distance

        return result
        '''
    @staticmethod
    def findResolutionsBetween(end_level, begin_level, resolutions):
        zoom_levels = []
        for resolution in resolutions:
            resolution = resolution[1]
            if(resolution <= end_level):
                if(resolution >= begin_level):
                    zoom_levels.append(resolution)
        
        return zoom_levels
    
    @staticmethod
    def findNearestResolution(resolutions, resolution):
        for i in range((len(resolutions)-1), 0, -1):
            max_resolution = max(resolutions[i][1], resolution)
            if not(max_resolution == resolution):
                return resolutions[i][1]
        
        return resolutions[(len(resolutions) - 1)][1]

    @staticmethod
    def getExtent(min_x, min_y, max_x, max_y):
        minx = min(min_x, max_x)
        miny = min(min_y, max_y)
        maxx = max(min_x, max_x)
        maxy = max(min_y, max_y)
        
        return Extent(minx, miny, maxx, maxy)
    
    #def getExtentWithinBounds(self, extentBounds, extent):
        #if
        
        